package com.it.park.rms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.rms.client.dto.MenuDto;
import com.it.park.rms.client.model.Menu;
import com.it.park.rms.client.model.Role;
import com.it.park.rms.client.model.RoleMenu;
import com.it.park.rms.client.service.RoleMenuService;
import com.it.park.rms.client.service.RoleService;
import com.it.park.rms.common.Page;
import com.it.park.rms.common.PageData;
import com.it.park.rms.common.Result;
import com.it.park.rms.constant.Constant;
import com.it.park.rms.constant.Global;

@Controller
@RequestMapping("/admin/role")
public class AdminRoleController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private RoleMenuService roleMenuService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<Role> listRows(HttpServletRequest request){
			PageData<Role> pageData = new PageData<>();
			Integer pageNow = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			Role role = new Role();
			Integer total = roleService.selectTotal(role);
			Page page = new Page(pageNow,total,rows);
			role.setStartPos(page.getStartPos());
			role.setPageSize(page.getPageSize());
			List<Role> list = roleService.selectPagination(role);
			pageData.setTotal(total);
			pageData.setRows(list);
			return pageData;
	}
	
	@RequestMapping("roleList")
	public String roleList(HttpServletRequest request){
			return Constant.ADMIN_PATH+"role/roleList";
	}
	
	/**
	 * 添加角色
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result<String> save(Role role,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		boolean success = roleService.insertSelective(role);
		if(success){
			json.setMessage(Global.SUCCESS_SAVE_MSG);
		}else{
			json.setSuccess(false);
			json.setMessage(Global.FAIL_SAVE_MSG);
		}
		return json;
	}
	
	/**
	 * 删除角色
	 * @return
	 */
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<String> deleteById(@RequestParam("id") int id,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		boolean success = roleService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage(Global.SUCCESS_DELETE_MSG);
			json.setSuccess(true);
		}else{
			json.setMessage(Global.FAIL_DELETE_MSG);
			json.setSuccess(false);
		}
		return json;
	}
	
	/**
	 * 修改角色
	 * @return
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Result<String> edit(Role role,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		boolean success = roleService.updateByPrimaryKeySelective(role);
		if(success){
			json.setMessage(Global.SUCCESS_UPDATE_MSG);
			json.setSuccess(true);
		}else{
			json.setMessage(Global.FAIL_UPDATE_MSG);
			json.setSuccess(false);
		}
		return json;
	}
	
	/**
	 * 用户页面角色信息
	 * */
	@RequestMapping("listRoles")
	@ResponseBody
	public List<Role> listRoles(){
		return roleService.selectAll();
	}
	
	@RequestMapping("saveOrEditAuthority")
	@ResponseBody
	public Result<Boolean> saveOrEditAuthority(@RequestBody MenuDto menuDto){
		 Result<Boolean> result = new Result<>(true);
		 Role role = menuDto.getRole();
		 List<Menu> menus = menuDto.getMenus();
		 List<RoleMenu> paramsList = new ArrayList<>();
		 RoleMenu roleMenu = null;
		 for (int i = 0; i < menus.size(); i++) {
			roleMenu = new RoleMenu();
			Menu menu =  menus.get(i);
			roleMenu.setRoleId(role.getId());
			roleMenu.setMenuId(menu.getId());
			paramsList.add(roleMenu);
		}
		boolean success =  roleMenuService.batchInsert(paramsList);
		if(!success){
			result.setSuccess(false);
			result.setMessage("保存失败");
		}
		return result;
	}
	
	@RequestMapping("exportWord")
	@ResponseBody
	public Result<Boolean> exportWord(){
		Result<Boolean> result = new Result<>(true);
		/*if(!success){
			result.setSuccess(false);
			result.setMessage("保存失败");
		}*/
		return result;
	}
	
}
